package com.cg.util;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

/**
 * Class for managing the configuration files (This class should not be instantiate)
 * 
 * 
 *
 */
public class PropertiesManager {
	
	private final static Properties GenericConfigProperties;
	private static Log log = null;
	private static String confDir; 
	
	public static Properties getGenericConfigProperties() {
		return GenericConfigProperties;
	}

	public static String getConfDir(){
		return confDir;
	}
	
	static {
		log = LogFactory.getLog(PropertiesManager.class.getName());
		
		GenericConfigProperties = new Properties();
		
		try {
			/*String propName = "configuration.path";
			confDir = System.getProperty(propName);
			
			if (confDir == null) 
				throw new Exception("The system property [" + propName+ "] is not set!");
			if(!confDir.endsWith(File.separator))
				confDir += File.separator;*/
			
			//GenericConfigProperties.loadFromXML(new FileInputStream(new File(confDir + "generic-config.xml")));
			GenericConfigProperties.loadFromXML(PropertiesManager.class.getResourceAsStream("/generic-config.xml"));
			
			for (final String attributeName : new String[] {"uri.searchService",
					"uri.baselineService"
					})
				if (!GenericConfigProperties.containsKey(attributeName))
					throw new Exception(attributeName + " property not found in generic-config.xml");

		} catch (Exception e) {
			try {
				log.error(e.toString().replaceAll("\t", " "));
				Logger.getLogger("ErrorLogger").error(e.toString(), e);
			} catch (Exception e1) {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				e1.printStackTrace(new PrintStream(out,true));
				log.error(out.toString().replaceAll("\t", " "));
			}
		}
	}

	
}

